package com.cosina.game.robodefence.ui.view;

import android.content.Context;
import android.graphics.Canvas;
import android.view.MotionEvent;
import android.view.View;

import com.cosina.game.robodefence.core.BattleEngine;
import com.cosina.game.robodefence.dao.MapsDao;
import com.cosina.game.robodefence.model.Finger;
import com.cosina.game.robodefence.ui.drawable.DrawManager;

public class GroundView extends View {

	public GroundView(Context context) {
		super(context);
		this.setBackgroundResource(MapsDao.getMapResId(BattleEngine.mapId));
	}

	@Override
	protected void onDraw(Canvas canvas) {
		DrawManager.drawTower(canvas);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if(BattleEngine.Status.running == BattleEngine.status){
			if(event.getAction() == MotionEvent.ACTION_DOWN){
				
				int x = (int)event.getX();
				int y = (int)event.getY();
				
				int tileX = x / 40;
				int tileY = y / 40;
				int[][] fieldData = BattleEngine.field.getFieldData();
				if(fieldData[tileY][tileX] > 0){
					Finger.select(tileX, tileY,fieldData[tileY][tileX]);
					return true;
				}else{
					if(Finger.isUpdating()){
						Finger.releaseSelect();
					}
				}
			}
		}
		return super.onTouchEvent(event);
	}
}
